﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Framework;
    using DotNetNuke.Services.Exceptions;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class AdminReportEdit : UserControlBase
    {
        [AccessedThroughProperty("cmdAddParam")]
        private LinkButton _cmdAddParam;
        [AccessedThroughProperty("cmdCancel")]
        private LinkButton _cmdCancel;
        [AccessedThroughProperty("cmdDelete")]
        private LinkButton _cmdDelete;
        [AccessedThroughProperty("cmdUpdate")]
        private LinkButton _cmdUpdate;
        [AccessedThroughProperty("chkAllowDisplay")]
        private CheckBox _chkAllowDisplay;
        [AccessedThroughProperty("chkAllowExport")]
        private CheckBox _chkAllowExport;
        [AccessedThroughProperty("chkDisplayInLine")]
        private CheckBox _chkDisplayInLine;
        [AccessedThroughProperty("chkEmailResults")]
        private CheckBox _chkEmailResults;
        [AccessedThroughProperty("chkSchedulerFlag")]
        private CheckBox _chkSchedulerFlag;
        [AccessedThroughProperty("chkShowSQL")]
        private CheckBox _chkShowSQL;
        [AccessedThroughProperty("ddlSchReRunMins")]
        private DropDownList _ddlSchReRunMins;
        [AccessedThroughProperty("ddlSchStartHour")]
        private DropDownList _ddlSchStartHour;
        [AccessedThroughProperty("ddlSchStartMins")]
        private DropDownList _ddlSchStartMins;
        [AccessedThroughProperty("dgParam")]
        private DataGrid _dgParam;
        [AccessedThroughProperty("Label1")]
        private Label _Label1;
        [AccessedThroughProperty("lblFieldDelimeter")]
        private Label _lblFieldDelimeter;
        [AccessedThroughProperty("lblFieldQualifier")]
        private Label _lblFieldQualifier;
        [AccessedThroughProperty("plAllowDisplay")]
        private UserControl _plAllowDisplay;
        [AccessedThroughProperty("plAllowExport")]
        private UserControl _plAllowExport;
        [AccessedThroughProperty("plDisplayInLine")]
        private UserControl _plDisplayInLine;
        [AccessedThroughProperty("plEmailFrom")]
        private UserControl _plEmailFrom;
        [AccessedThroughProperty("plEmailResults")]
        private UserControl _plEmailResults;
        [AccessedThroughProperty("plEmailTo")]
        private UserControl _plEmailTo;
        [AccessedThroughProperty("plReportName")]
        private UserControl _plReportName;
        [AccessedThroughProperty("plReportRef")]
        private UserControl _plReportRef;
        [AccessedThroughProperty("plReportTitle")]
        private UserControl _plReportTitle;
        [AccessedThroughProperty("plSchedulerFlag")]
        private UserControl _plSchedulerFlag;
        [AccessedThroughProperty("plSchReRunMins")]
        private UserControl _plSchReRunMins;
        [AccessedThroughProperty("plSchStartHour")]
        private UserControl _plSchStartHour;
        [AccessedThroughProperty("plSchStartMins")]
        private UserControl _plSchStartMins;
        [AccessedThroughProperty("plShowSQL")]
        private UserControl _plShowSQL;
        [AccessedThroughProperty("plSQLText")]
        private UserControl _plSQLText;
        protected int _RepID = -1;
        [AccessedThroughProperty("txtAddParams")]
        private TextBox _txtAddParams;
        [AccessedThroughProperty("txtEmailFrom")]
        private TextBox _txtEmailFrom;
        [AccessedThroughProperty("txtEmailTo")]
        private TextBox _txtEmailTo;
        [AccessedThroughProperty("txtFieldDelimeter")]
        private TextBox _txtFieldDelimeter;
        [AccessedThroughProperty("txtFieldQualifier")]
        private TextBox _txtFieldQualifier;
        [AccessedThroughProperty("txtReportName")]
        private TextBox _txtReportName;
        [AccessedThroughProperty("txtReportRef")]
        private TextBox _txtReportRef;
        [AccessedThroughProperty("txtReportTitle")]
        private TextBox _txtReportTitle;
        [AccessedThroughProperty("txtSQLText")]
        private TextBox _txtSQLText;

        public event AddParamButtonEventHandler AddParamButton;

        public event CancelButtonEventHandler CancelButton;

        public event DeleteButtonEventHandler DeleteButton;

        public event UpdateButtonEventHandler UpdateButton;

        private void cmdAddParam_Click(object sender, EventArgs e)
        {
            try
            {
                SQLReportController controller = new SQLReportController();
                this._RepID = this.SaveReport(this._RepID);
                if (Versioned.IsNumeric(this.txtAddParams.Text))
                {
                    int num2 = Conversions.ToInteger(this.txtAddParams.Text);
                    for (int i = 1; i <= num2; i++)
                    {
                        NB_Store_SQLReportParamInfo objInfo = new NB_Store_SQLReportParamInfo {
                            ParamName = "",
                            ParamType = "nvarchar(50)",
                            ParamValue = "",
                            ReportID = this._RepID,
                            ReportParamID = -1,
                            ParamSource = 1
                        };
                        controller.UpdateObjSQLReportParam(objInfo);
                    }
                    
                    if (AddParamButton != null)
                    {
                        AddParamButton(RuntimeHelpers.GetObjectValue(sender), e);
                    }
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            try
            {
                
                if (CancelButton != null)
                {
                    CancelButton(RuntimeHelpers.GetObjectValue(sender), e);
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this._RepID >= 0)
                {
                    new SQLReportController().DeleteSQLReport(this._RepID);
                }
                
                if (DeleteButton != null)
                {
                    DeleteButton(RuntimeHelpers.GetObjectValue(sender), e);
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void cmdUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                this.SaveReport(this._RepID);
                
                if (this.UpdateButton != null)
                {
                    UpdateButton(RuntimeHelpers.GetObjectValue(sender), e);
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void dgParam_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            try
            {
                int reportParamID = int.Parse(e.CommandArgument.ToString());
                new SQLReportController().DeleteSQLReportParam(reportParamID);
                this.PopulateParams(this.RepID);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
                ProjectData.ClearProjectError();
            }
        }

        public void populateEdit(int ReportID)
        {
            NB_Store_SQLReportInfo sQLReport = new SQLReportController().GetSQLReport(ReportID);
            if (sQLReport != null)
            {
                this.chkAllowDisplay.Checked = sQLReport.AllowDisplay;
                this.chkAllowExport.Checked = sQLReport.AllowExport;
                this.chkDisplayInLine.Checked = sQLReport.DisplayInLine;
                this.txtEmailFrom.Text = sQLReport.EmailFrom;
                this.chkEmailResults.Checked = sQLReport.EmailResults;
                this.txtEmailTo.Text = sQLReport.EmailTo;
                this.txtReportName.Text = sQLReport.ReportName;
                this.chkSchedulerFlag.Checked = sQLReport.SchedulerFlag;
                this.ddlSchReRunMins.SelectedValue = sQLReport.SchReRunMins;
                this.ddlSchStartHour.SelectedValue = sQLReport.SchStartHour;
                this.ddlSchStartMins.SelectedValue = sQLReport.SchStartMins;
                this.txtSQLText.Text = sQLReport.SQL;
                this.chkShowSQL.Checked = sQLReport.ShowSQL;
                if (sQLReport.ReportRef == "")
                {
                    this.txtReportRef.Text = "R" + sQLReport.ReportID.ToString();
                }
                else
                {
                    this.txtReportRef.Text = sQLReport.ReportRef;
                }
                this.txtReportTitle.Text = sQLReport.ReportTitle;
                this.txtFieldDelimeter.Text = sQLReport.FieldDelimeter;
                if (this.txtFieldDelimeter.Text == "")
                {
                    this.txtFieldDelimeter.Text = ",";
                }
                this.txtFieldQualifier.Text = sQLReport.FieldQualifier;
                this.PopulateParams(ReportID);
            }
            else
            {
                this.cmdDelete.Visible = false;
            }
        }

        private void PopulateParams(int ReportID)
        {
            ArrayList sQLReportParamList = new SQLReportController().GetSQLReportParamList(ReportID);
            this.dgParam.DataSource = sQLReportParamList;
            this.dgParam.DataBind();
        }

        private int SaveReport(int ReportID)
        {
            SQLReportController controller = new SQLReportController();
            NB_Store_SQLReportInfo sQLReport = controller.GetSQLReport(ReportID);
            if (sQLReport == null)
            {
                sQLReport = new NB_Store_SQLReportInfo();
            }
            sQLReport.AllowDisplay = this.chkAllowDisplay.Checked;
            sQLReport.AllowExport = this.chkAllowExport.Checked;
            sQLReport.DisplayInLine = this.chkDisplayInLine.Checked;
            sQLReport.EmailFrom = this.txtEmailFrom.Text;
            sQLReport.EmailResults = this.chkEmailResults.Checked;
            sQLReport.EmailTo = this.txtEmailTo.Text;
            sQLReport.LastRunTime = DateAndTime.Today;
            sQLReport.PortalID = this.PortalSettings.PortalId;
            sQLReport.ReportID = ReportID;
            sQLReport.ReportName = this.txtReportName.Text;
            sQLReport.SchedulerFlag = this.chkSchedulerFlag.Checked;
            sQLReport.SchReRunMins = this.ddlSchReRunMins.SelectedValue;
            sQLReport.SchStartHour = this.ddlSchStartHour.SelectedValue;
            sQLReport.SchStartMins = this.ddlSchStartMins.SelectedValue;
            sQLReport.SQL = this.txtSQLText.Text;
            sQLReport.ShowSQL = this.chkShowSQL.Checked;
            sQLReport.AllowPaging = false;
            sQLReport.ReportRef = this.txtReportRef.Text;
            sQLReport.ReportTitle = this.txtReportTitle.Text;
            sQLReport.FieldDelimeter = this.txtFieldDelimeter.Text;
            if (sQLReport.FieldDelimeter == "")
            {
                sQLReport.FieldDelimeter = ",";
            }
            sQLReport.FieldQualifier = this.txtFieldQualifier.Text;
            int num = controller.UpdateObjSQLReport(sQLReport);
            this.updateParams();
            return num;
        }

        private void updateParams()
        {
            IEnumerator enumerator=null;
            SQLReportController controller = new SQLReportController();
            try
            {
                enumerator = this.dgParam.Items.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    DataGridItem current = (DataGridItem) enumerator.Current;
                    int reportParamID = Conversions.ToInteger(current.Cells[0].Text);
                    NB_Store_SQLReportParamInfo sQLReportParam = controller.GetSQLReportParam(reportParamID);
                    if (sQLReportParam != null)
                    {
                        sQLReportParam.ParamName = ((TextBox) current.FindControl("txtParamName")).Text;
                        sQLReportParam.ParamSource = Conversions.ToInteger(((DropDownList) current.FindControl("ddlSource")).SelectedValue);
                        sQLReportParam.ParamType = ((DropDownList) current.FindControl("ddlType")).SelectedValue;
                        sQLReportParam.ParamValue = ((TextBox) current.FindControl("txtParamValue")).Text;
                        controller.UpdateObjSQLReportParam(sQLReportParam);
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        protected virtual LinkButton cmdAddParam
        {
            get
            {
                return this._cmdAddParam;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdAddParam_Click);
                if (this._cmdAddParam != null)
                {
                    this._cmdAddParam.Click -= handler;
                }
                this._cmdAddParam = value;
                if (this._cmdAddParam != null)
                {
                    this._cmdAddParam.Click += handler;
                }
            }
        }

        protected virtual LinkButton cmdCancel
        {
            get
            {
                return this._cmdCancel;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdCancel_Click);
                if (this._cmdCancel != null)
                {
                    this._cmdCancel.Click -= handler;
                }
                this._cmdCancel = value;
                if (this._cmdCancel != null)
                {
                    this._cmdCancel.Click += handler;
                }
            }
        }

        protected virtual LinkButton cmdDelete
        {
            get
            {
                return this._cmdDelete;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdDelete_Click);
                if (this._cmdDelete != null)
                {
                    this._cmdDelete.Click -= handler;
                }
                this._cmdDelete = value;
                if (this._cmdDelete != null)
                {
                    this._cmdDelete.Click += handler;
                }
            }
        }

        protected virtual LinkButton cmdUpdate
        {
            get
            {
                return this._cmdUpdate;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdUpdate_Click);
                if (this._cmdUpdate != null)
                {
                    this._cmdUpdate.Click -= handler;
                }
                this._cmdUpdate = value;
                if (this._cmdUpdate != null)
                {
                    this._cmdUpdate.Click += handler;
                }
            }
        }

        protected virtual CheckBox chkAllowDisplay
        {
            get
            {
                return this._chkAllowDisplay;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._chkAllowDisplay = value;
            }
        }

        protected virtual CheckBox chkAllowExport
        {
            get
            {
                return this._chkAllowExport;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._chkAllowExport = value;
            }
        }

        protected virtual CheckBox chkDisplayInLine
        {
            get
            {
                return this._chkDisplayInLine;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._chkDisplayInLine = value;
            }
        }

        protected virtual CheckBox chkEmailResults
        {
            get
            {
                return this._chkEmailResults;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._chkEmailResults = value;
            }
        }

        protected virtual CheckBox chkSchedulerFlag
        {
            get
            {
                return this._chkSchedulerFlag;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._chkSchedulerFlag = value;
            }
        }

        protected virtual CheckBox chkShowSQL
        {
            get
            {
                return this._chkShowSQL;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._chkShowSQL = value;
            }
        }

        protected virtual DropDownList ddlSchReRunMins
        {
            get
            {
                return this._ddlSchReRunMins;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._ddlSchReRunMins = value;
            }
        }

        protected virtual DropDownList ddlSchStartHour
        {
            get
            {
                return this._ddlSchStartHour;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._ddlSchStartHour = value;
            }
        }

        protected virtual DropDownList ddlSchStartMins
        {
            get
            {
                return this._ddlSchStartMins;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._ddlSchStartMins = value;
            }
        }

        protected virtual DataGrid dgParam
        {
            get
            {
                return this._dgParam;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                DataGridCommandEventHandler handler = new DataGridCommandEventHandler(this.dgParam_DeleteCommand);
                if (this._dgParam != null)
                {
                    this._dgParam.DeleteCommand -= handler;
                }
                this._dgParam = value;
                if (this._dgParam != null)
                {
                    this._dgParam.DeleteCommand += handler;
                }
            }
        }

        protected virtual Label Label1
        {
            get
            {
                return this._Label1;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._Label1 = value;
            }
        }

        protected virtual Label lblFieldDelimeter
        {
            get
            {
                return this._lblFieldDelimeter;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblFieldDelimeter = value;
            }
        }

        protected virtual Label lblFieldQualifier
        {
            get
            {
                return this._lblFieldQualifier;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblFieldQualifier = value;
            }
        }

        protected virtual UserControl plAllowDisplay
        {
            get
            {
                return this._plAllowDisplay;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plAllowDisplay = value;
            }
        }

        protected virtual UserControl plAllowExport
        {
            get
            {
                return this._plAllowExport;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plAllowExport = value;
            }
        }

        protected virtual UserControl plDisplayInLine
        {
            get
            {
                return this._plDisplayInLine;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plDisplayInLine = value;
            }
        }

        protected virtual UserControl plEmailFrom
        {
            get
            {
                return this._plEmailFrom;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plEmailFrom = value;
            }
        }

        protected virtual UserControl plEmailResults
        {
            get
            {
                return this._plEmailResults;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plEmailResults = value;
            }
        }

        protected virtual UserControl plEmailTo
        {
            get
            {
                return this._plEmailTo;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plEmailTo = value;
            }
        }

        protected virtual UserControl plReportName
        {
            get
            {
                return this._plReportName;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plReportName = value;
            }
        }

        protected virtual UserControl plReportRef
        {
            get
            {
                return this._plReportRef;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plReportRef = value;
            }
        }

        protected virtual UserControl plReportTitle
        {
            get
            {
                return this._plReportTitle;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plReportTitle = value;
            }
        }

        protected virtual UserControl plSchedulerFlag
        {
            get
            {
                return this._plSchedulerFlag;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plSchedulerFlag = value;
            }
        }

        protected virtual UserControl plSchReRunMins
        {
            get
            {
                return this._plSchReRunMins;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plSchReRunMins = value;
            }
        }

        protected virtual UserControl plSchStartHour
        {
            get
            {
                return this._plSchStartHour;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plSchStartHour = value;
            }
        }

        protected virtual UserControl plSchStartMins
        {
            get
            {
                return this._plSchStartMins;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plSchStartMins = value;
            }
        }

        protected virtual UserControl plShowSQL
        {
            get
            {
                return this._plShowSQL;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plShowSQL = value;
            }
        }

        protected virtual UserControl plSQLText
        {
            get
            {
                return this._plSQLText;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._plSQLText = value;
            }
        }

        public int RepID
        {
            get
            {
                return this._RepID;
            }
            set
            {
                this._RepID = value;
            }
        }

        protected virtual TextBox txtAddParams
        {
            get
            {
                return this._txtAddParams;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtAddParams = value;
            }
        }

        protected virtual TextBox txtEmailFrom
        {
            get
            {
                return this._txtEmailFrom;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtEmailFrom = value;
            }
        }

        protected virtual TextBox txtEmailTo
        {
            get
            {
                return this._txtEmailTo;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtEmailTo = value;
            }
        }

        protected virtual TextBox txtFieldDelimeter
        {
            get
            {
                return this._txtFieldDelimeter;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtFieldDelimeter = value;
            }
        }

        protected virtual TextBox txtFieldQualifier
        {
            get
            {
                return this._txtFieldQualifier;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtFieldQualifier = value;
            }
        }

        protected virtual TextBox txtReportName
        {
            get
            {
                return this._txtReportName;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtReportName = value;
            }
        }

        protected virtual TextBox txtReportRef
        {
            get
            {
                return this._txtReportRef;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtReportRef = value;
            }
        }

        protected virtual TextBox txtReportTitle
        {
            get
            {
                return this._txtReportTitle;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtReportTitle = value;
            }
        }

        protected virtual TextBox txtSQLText
        {
            get
            {
                return this._txtSQLText;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtSQLText = value;
            }
        }

        public delegate void AddParamButtonEventHandler(object sender, EventArgs e);

        public delegate void CancelButtonEventHandler(object sender, EventArgs e);

        public delegate void DeleteButtonEventHandler(object sender, EventArgs e);

        public delegate void UpdateButtonEventHandler(object sender, EventArgs e);
    }
}

